package com.pgcomb.poweriot.alarmservice.alarm.service;

import com.pgcomb.common.bean.ResultBean;
import com.pgcomb.poweriot.alarmservice.enums.AlarmStatusEnum;
import com.pgcomb.poweriot.alarmservice.mq.AlarmMqttSend;
import com.pgcomb.poweriot.alarmservice.vo.AlarmMqtt;
import com.pgcomb.web.dao.AlarmInfo;
import com.pgcomb.web.dao.Equipment;
import com.pgcomb.web.dao.Park;
import com.pgcomb.web.dao.Room;
import com.pgcomb.web.repo.AlarmInfoRepo;
import com.pgcomb.web.repo.EquipmentRepo;
import com.pgcomb.web.repo.ParkRepo;
import com.pgcomb.web.repo.RoomRepo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

import static com.pgcomb.poweriot.alarmservice.enums.AlarmStatusEnum.OFF;
import static com.pgcomb.poweriot.alarmservice.enums.AlarmStatusEnum.ON;

/**
 * @author zhengheqi
 * @version 1.0.0
 * @Description: TODO
 * @Date 2021/7/7 16:37
 */
@Slf4j
@Service
public class MqttAlarmService {
    @Resource
    private AlarmInfoRepo alarmSendRepo;
    @Resource
    private EquipmentRepo equipmentRepo;
    @Resource
    private ParkRepo parkRepo;
    @Resource
    private RoomRepo roomRepo;
    @Resource
    private AlarmMqttSend alarmMqttSend;
    @Resource
    private KeyService keyService;

    @Value("${alarm.sendMsg.re-send-interval}")
    private long reSendInterval;

    public ResultBean<?> send(AlarmMqtt alarmMqtt) {
        log.info("mqtt告警参数alarmMqtt:{}", alarmMqtt);
        String equipmentCode = alarmMqtt.getEquipmentCode();
        String alarmType = alarmMqtt.getAlarmType();
        String alarmStatus = alarmMqtt.getAlarmStatus();

        Equipment equipment = equipmentRepo.findByEquipmentCode(equipmentCode);
        if (ObjectUtils.isEmpty(equipment)) {
            log.error("alarmInfo save error : equipment [{}] is not exist", equipmentCode);
            return ResultBean.error("alarmInfo save error : equipment [{}] is not exist", equipmentCode);
        }

        //判断是否需要保存告警信息 ---------------------------------------------------
        AlarmInfo alarmInfo = alarmSendRepo.findByEquipmentIdAndAlarmTypeAndAlarmStatus(equipment.getId(), alarmType, ON.getCode());
        log.info("alarmInfo:{}", alarmInfo);

        //无告警记录，切本次告警状态为开启则需要新增记录
        if (ObjectUtils.isEmpty(alarmInfo) && ON.getCode().equals(alarmStatus)) {
            alarmInfo = new AlarmInfo();

            Room room = roomRepo.findById(equipment.getRoomId()).orElse(null);
            if (ObjectUtils.isEmpty(room)) {
                log.error("alarmInfo save error : room is not exist");
                return ResultBean.error("alarmInfo save error : room is not exist");
            }
            //构造告警信息
            Park park = parkRepo.findById(room.getParkId()).orElse(null);
            if (ObjectUtils.isEmpty(park)) {
                log.error("alarmInfo save error : park is not exist");
                return ResultBean.error("alarmInfo save error : park is not exist");
            }

            alarmInfo.setId(Long.valueOf(keyService.alarmMsg()));
            alarmInfo.setAlarmStatus(ON.getCode());
            alarmInfo.setAlarmStartTime(LocalDateTime.now());
            alarmInfo.setParkId(park.getId());
            alarmInfo.setParkName(park.getParkName());
            alarmInfo.setRoomId(room.getId());
            alarmInfo.setBoxId(equipment.getBoxId());
            alarmInfo.setEquipmentId(equipment.getId());
            alarmInfo.setEquipmentCode(equipmentCode);
            alarmInfo.setEquipmentName(equipment.getEquipmentName());
            alarmInfo.setDrawerId(equipment.getDrawerId());
            alarmInfo.setContactsIds(equipment.getContactsIds());
            alarmInfo.setAlarmMsg(alarmMqtt.getAlarmMsg());
            alarmInfo.setAlarmType(alarmType);

        } else if (!ObjectUtils.isEmpty(alarmInfo) && OFF.getCode().equals(alarmStatus)) {
            //数据库有告警记录，且本次告警状态为正常，则需要更新告警状态为告警解除
            alarmInfo.setAlarmStatus(OFF.getCode());
            alarmInfo.setAlarmEndTime(LocalDateTime.now());

        } else {
            log.info("持续告警信息，无需记录");
            return ResultBean.ok("无需处理");
        }

        AlarmInfo save = alarmSendRepo.save(alarmInfo);

        // 发送告警消息给ES
//        alarmMqttSend.sendAlarm2MQ(alarmSendInfo);

        return ResultBean.ok();
    }

}
